﻿#include "VideoController.h"

VideoController::VideoController(QObject *parent)
    : QObject(parent)
{
    m_renderer = new VideoRenderer();

    connect(&m_decoder, &VideoDecoder::frameReceived,
            m_renderer, &VideoRenderer::updateFrame);

    connect(&m_decoder, &VideoDecoder::durationChanged, this, [this](int ms){
        m_duration = ms;
        emit durationChanged(ms);
    });

    connect(&m_decoder, &VideoDecoder::positionChanged, this, [this](int ms){
        m_position = qMin(m_position + ms, m_duration);
        emit positionChanged(m_position);
    });

    m_bPlaying = false;
}

void VideoController::play(const QString &filePath) {
    m_decoder.open(filePath);
    m_bPlaying = true;
}

void VideoController::stop() {
    m_bPlaying = false;
    m_decoder.stop();
    m_position = 0;
    emit positionChanged(0);
}

void VideoController::seek(int position) {
    m_position = qBound(0, position, m_duration);
    m_decoder.setPosition(m_position);
    emit positionChanged(m_position);
}

bool VideoController::isPlay(){
    return m_bPlaying;
}
